package dao;

import java.sql.SQLException;
import java.util.ArrayList;

import tools.Tool;
import Model.ShowBlogMessage;

public class ShowblogMessageDAO extends DAO {
	private String classname=this.getClass().getName();
	public ShowblogMessageDAO()
	{
		super();
	}
	public ShowBlogMessage getAMessageByblogid(String blogid)
	{
		ShowBlogMessage show=new ShowBlogMessage();
		sql="select blog.id,text,source.uptime,source.id,saveurl,upname,uptype from blog,source where blog.sourceid=source.id and blog.id=? and isshow='1' ";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, blogid);
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				show.setBlogid(blogid);
				show.setSourceid(rs.getString(4));
				show.setSourcename(rs.getString("upname"));
				show.setSourceurl(rs.getString("saveurl"));
				show.setText(rs.getString("text"));
				show.setUptime(rs.getString("uptime"));
				show.setUptype(rs.getString("uptype"));
				String message=rs.getString("text");
				System.out.println(message);
				//System.out.println(newmessage);
				show.setShortmessage(rs.getString("text"));
				return show;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return null;
	}
	public ArrayList<ShowBlogMessage> getAllShowMessage()
	{
		ArrayList<ShowBlogMessage> all=new ArrayList<ShowBlogMessage>();
		sql="select blog.id,text,source.uptime,source.id,saveurl,newname,uptype from blog,source where blog.sourceid=source.id and blog.id like ? order by source.uptime desc";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, "blog_%");
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				ShowBlogMessage show=new ShowBlogMessage();
				show.setBlogid(rs.getString(1));
				show.setSourceid(rs.getString(4));
				show.setSourcename(rs.getString("newname"));
				show.setSourceurl(rs.getString("saveurl"));
				show.setText(rs.getString("text"));
				show.setUptime(rs.getString("uptime"));
				show.setUptype(rs.getString("uptype"));
				show.setShortmessage(rs.getString("text"));
				all.add(show);
			}
			
			if(!rs.next())
			{
				return all;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return null;
	}
	
}
